package com.admin.web.controller;

import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.goods.ListClassify;
import com.bayss.core.service.ListClassifyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * list classify
 */
@RestController
public class ListClassifyController  extends ApiBaseController{
    @Autowired
    private ListClassifyService listClassifyService;

    /**
     * 大类删除
     * @param id
     * @return
     */
    @GetMapping(value = "/listClassify/delete.json")
    public ResultMsg delete(Integer id) {
        try {
            listClassifyService.deleteById(id);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "删除失败");
        }
    }

    /**
     * 大类新增
     * @param listClassify
     * @return
     */
    @PostMapping(value = "/listClassify/add.json")
    public ResultMsg add(@RequestBody ListClassify listClassify) {
        return listClassifyService.add(listClassify);
    }

    /**
     * 大类修改
     * @param listClassify
     * @return
     */
    @PostMapping(value = "/listClassify/update.json")
    public ResultMsg update(@RequestBody ListClassify listClassify) {
        listClassifyService.update(listClassify);
        return ResultMsg.success();
    }


    /**
     * 大类修改
     * @param id
     * @return
     */
    @PostMapping(value = "/listClassify/default.json")
    public ResultMsg defaultCheck(Integer id) {
        boolean b = listClassifyService.defaultCheck(id);
        return ResultMsg.success(b);
    }

    /**
     * 大类分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/listClassify/getByPage.json")
    public ResultMsg getByPage(Integer page, Integer pageSize) {
        try {
            PageInfo<ListClassify> pageInfo = listClassifyService.findByPage(page, pageSize);
            return ResultMsg.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "查询失败");
        }
    }

    /**
     * 大类查询全部
     * @return
     */
    @GetMapping(value = "/listClassify/list.json")
    public ResultMsg list() {
        try {
            List<ListClassify> list = listClassifyService.list();
            return ResultMsg.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "查询失败");
        }
    }
}
